import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../ActionManager/ActionArgHelper";
import { ETaskState } from "GameLoop/DecisionMaking/TaskDeclare/type";
 

/*
使用Power
*/
export class State_UserPower<T extends IStateArg<PowerCreep>>  extends State<T>
{

    public  getActions(arg:T):Action[]
    {

        if(!arg.entity.memory.taskid)
        {
            return []
        }
        const toptask =  arg.entity.ownerRoom().findTaskById<ITaskDeclare['pc']>(arg.entity.memory.taskid);
        
        if(toptask)
        {
            const powerinfo:{
                className: PowerClassConstant;
                level: number[];
                cooldown: number;
                effect?: number[];
                range?: number;
                ops?: number | number[];
                duration?: number | number[];
            } = POWER_INFO[toptask.data.arg.POWER];


            if(toptask.data.arg.ObjectID)
            {

                const userPowerTarget = Game.getObjectById<RoomObject&_HasId>(toptask.data.arg.ObjectID);

                if(userPowerTarget )
                {

                    if(userPowerTarget.effects)
                    {
                        const index = userPowerTarget.effects.findIndex(value=>value.effect == toptask.data.arg.POWER)
                        if(index!=-1)
                        {
                            // 已经有特效了
                            toptask.data.state = ETaskState.Complete;
                            
                            arg.entity.log(`warn`,`Power完成`,toptask.data.arg.POWER)
                            return [];
                        }
                    }
                    const power = arg.entity.powers[ toptask.data.arg.POWER];
                    if(power.cooldown && power.cooldown>10)
                    {
                        // arg.entity.popTask();
                        toptask.data.state = ETaskState.Complete;
                        // 冷却中
                        arg.entity.log(`warn`,`PowerCD大于10`,toptask.data.arg.POWER,power.cooldown)
                        return [];
                    }

                    if(powerinfo.range && arg.entity.pos.distance(userPowerTarget.pos)>powerinfo.range)
                    {

                        return [ActionArgHelper.actionMoveto(arg.entity,{pos:userPowerTarget.pos,range:powerinfo.range},{})]
                    }
                    else
                    {

                        // 没有距离限制

                        if(!power.cooldown)
                        {
                            arg.entity.usePower( toptask.data.arg.POWER,userPowerTarget);
                            toptask.data.state = ETaskState.Complete;
                            // arg.entity.popTask();
                            arg.entity.log(`warn`,`Power完成`,toptask.data.arg.POWER)
                            return [];
                        }


                    }

                }
                else
                {
                    toptask.data.state = ETaskState.Complete;
                    // Object 对象已经没有了
                    arg.entity.log(`warn`,`对象已经没有`,toptask.data.arg.POWER)
                     
                }
            }
            else
            {
                toptask.data.state = ETaskState.Complete;
                // Object 对象已经没有了
                arg.entity.log(`warn`,`对象已经没有`,toptask.data.arg.POWER)
            }
        }
        return [];
    }

}
